button: draw surrounding lines
This commit is contained in:
parent
3d6f4f3af1
commit
fa744aa732
|
@ -33,6 +33,6 @@ static inline SDL_Rect operator+(const SDL_Rect &A, const SDL_Rect &B) {
|
|||
return Rect(A.x + B.x, A.y + B.y, A.w + B.w, A.h + B.h);
|
||||
}
|
||||
|
||||
void draw_thicker_line(SDL_Renderer *renderer, const SDL_Point *point, int n);
|
||||
void draw_surrounding_line(SDL_Renderer *renderer, SDL_Rect r, int gap);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <nvboard.h>
|
||||
#include <render.h>
|
||||
|
||||
#define BTNC_X 520
|
||||
#define BTNC_Y 250
|
||||
|
@ -7,17 +8,46 @@
|
|||
#define BTNC_HEIGHT 30
|
||||
|
||||
static const SDL_Rect btn_rects[6] = {
|
||||
{BTNC_X, BTNC_Y, BTNC_WIDTH, BTNC_HEIGHT },
|
||||
{BTNC_X, BTNC_Y - BTNC_HEIGHT - BTNC_SEP, BTNC_WIDTH, BTNC_HEIGHT },
|
||||
{BTNC_X, BTNC_Y + BTNC_HEIGHT + BTNC_SEP, BTNC_WIDTH, BTNC_HEIGHT },
|
||||
{BTNC_X - BTNC_WIDTH - BTNC_SEP, BTNC_Y, BTNC_WIDTH, BTNC_HEIGHT },
|
||||
{BTNC_X + BTNC_WIDTH + BTNC_SEP, BTNC_Y, BTNC_WIDTH, BTNC_HEIGHT },
|
||||
{BTNC_X, BTNC_Y, BTNC_WIDTH, BTNC_HEIGHT }, // C
|
||||
{BTNC_X, BTNC_Y - BTNC_HEIGHT - BTNC_SEP, BTNC_WIDTH, BTNC_HEIGHT }, // U
|
||||
{BTNC_X, BTNC_Y + BTNC_HEIGHT + BTNC_SEP, BTNC_WIDTH, BTNC_HEIGHT }, // D
|
||||
{BTNC_X - BTNC_WIDTH - BTNC_SEP, BTNC_Y, BTNC_WIDTH, BTNC_HEIGHT }, // L
|
||||
{BTNC_X + BTNC_WIDTH + BTNC_SEP, BTNC_Y, BTNC_WIDTH, BTNC_HEIGHT }, // R
|
||||
{BTNC_X + BTNC_WIDTH + BTNC_SEP/2, BTNC_Y - 2 * (BTNC_HEIGHT + BTNC_SEP), BTNC_WIDTH, BTNC_HEIGHT } // RST, but not draw
|
||||
};
|
||||
|
||||
static void init_render_local(SDL_Renderer *renderer) {
|
||||
// draw surrounding lines
|
||||
const int gap = 8;
|
||||
int w = (BTNC_WIDTH + BTNC_SEP) * 3 - BTNC_SEP;
|
||||
draw_surrounding_line(renderer, Rect(btn_rects[3].x, btn_rects[1].y, w, w), gap);
|
||||
|
||||
// draw indices for each button
|
||||
const char *str = "CUDLR";
|
||||
for (int i = 0; i < 5; i ++) {
|
||||
SDL_Texture *t = ch2texture(renderer, str[i], 0xffffff, BOARD_BG_COLOR);
|
||||
SDL_Point p = Point(btn_rects[i].x, btn_rects[i].y) + Point(BTNC_WIDTH + 2, BTNC_HEIGHT / 2)
|
||||
- Point(0, CH_HEIGHT / 2);
|
||||
SDL_Rect r = Rect(p, CH_WIDTH, CH_HEIGHT);
|
||||
SDL_RenderCopy(renderer, t, NULL, &r);
|
||||
SDL_DestroyTexture(t);
|
||||
}
|
||||
|
||||
// draw the title
|
||||
str = "Button Pad";
|
||||
SDL_Texture *t = str2texture(renderer, str, 0xffffff, BOARD_BG_COLOR);
|
||||
int w0 = CH_WIDTH * strlen(str);
|
||||
SDL_Point p = Point(btn_rects[3].x, btn_rects[1].y) - Point(0, gap) - Point(0, CH_HEIGHT / 2)
|
||||
+ Point(w / 2, 0) - Point(w0 / 2, 0);
|
||||
SDL_Rect r = Rect(p, w0, CH_HEIGHT);
|
||||
SDL_RenderCopy(renderer, t, NULL, &r);
|
||||
SDL_DestroyTexture(t);
|
||||
}
|
||||
|
||||
void init_button(SDL_Renderer *renderer) {
|
||||
SDL_Texture *tbutton_on = load_pic_texture(renderer, VBTN_ON_PATH);
|
||||
SDL_Texture *tbutton_off = load_pic_texture(renderer, VBTN_OFF_PATH);
|
||||
init_render_local(renderer);
|
||||
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
Component *ptr = new Component(renderer, 2, 0, BUTTON_TYPE);
|
||||
|
|
|
@ -28,7 +28,7 @@ SDL_Texture* new_texture(SDL_Renderer *renderer, int w, int h, int r, int g, int
|
|||
return surface2texture(renderer, s);
|
||||
}
|
||||
|
||||
void draw_thicker_line(SDL_Renderer *renderer, const SDL_Point *point, int n) {
|
||||
static void draw_thicker_line(SDL_Renderer *renderer, const SDL_Point *point, int n) {
|
||||
SDL_RenderDrawLines(renderer, point, n);
|
||||
|
||||
SDL_Point *p = new SDL_Point[n];
|
||||
|
@ -39,6 +39,26 @@ void draw_thicker_line(SDL_Renderer *renderer, const SDL_Point *point, int n) {
|
|||
delete [] p;
|
||||
}
|
||||
|
||||
void draw_surrounding_line(SDL_Renderer *renderer, SDL_Rect r,
|
||||
int gap) { // gap between surrounding lines and component
|
||||
SDL_Point top_left = Point(r.x, r.y) + Point(-gap, -gap);
|
||||
const int w = r.w + gap * 2;
|
||||
const int h = r.h + gap * 2;
|
||||
const int d = 12;
|
||||
SDL_Point p[9];
|
||||
p[0] = top_left + Point(d, 0);
|
||||
p[1] = p[0] + Point(w - 2 * d, 0);
|
||||
p[2] = p[1] + Point(d, d);
|
||||
p[3] = p[2] + Point(0, h - 2 * d);
|
||||
p[4] = p[3] + Point(-d, d);
|
||||
p[5] = p[4] - Point(w - 2 * d, 0);
|
||||
p[6] = p[5] - Point(d, d);
|
||||
p[7] = p[6] - Point(0, h - 2 * d);
|
||||
p[8] = p[0];
|
||||
draw_thicker_line(renderer, p, 9);
|
||||
}
|
||||
|
||||
|
||||
void init_render(SDL_Renderer *renderer) {
|
||||
nvboard_home = getenv("NVBOARD_HOME");
|
||||
|
||||
|
|
|
@ -95,43 +95,27 @@ static void init_render_local(SDL_Renderer *renderer) {
|
|||
#endif
|
||||
|
||||
// draw surrounding lines
|
||||
SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0);
|
||||
const int gap = 14; // gap between surrounding lines and the 7-segments pad
|
||||
SDL_Point top_left = Point(SEG_X, SEG_Y) + Point(-gap, -gap);
|
||||
const int w = SEG_TOT_WIDTH + gap * 2;
|
||||
const int h = SEG_TOT_HEIGHT + gap * 2;
|
||||
const int ch_pad_h = 20;
|
||||
const int ch_pad_w = 12;
|
||||
const int d = 12;
|
||||
SDL_Point p[32];
|
||||
p[0] = top_left + Point(d, 0);
|
||||
p[1] = p[0] + Point(w - 2 * d, 0);
|
||||
p[2] = p[1] + Point(d, d);
|
||||
p[3] = p[2] + Point(0, h - 2 * d);
|
||||
p[4] = p[3] + Point(-d, d);
|
||||
p[5] = p[4] - Point(w - 2 * d, 0);
|
||||
p[6] = p[5] - Point(d, d);
|
||||
p[7] = p[6] - Point(0, h - 2 * d);
|
||||
p[8] = p[0];
|
||||
draw_thicker_line(renderer, p, 9);
|
||||
const int gap = 14;
|
||||
draw_surrounding_line(renderer, Rect(SEG_X, SEG_Y, SEG_TOT_WIDTH, SEG_TOT_HEIGHT), gap);
|
||||
|
||||
char buf[2] = "0";
|
||||
SDL_Point p0 = Point(SEG_X, SEG_Y) + Point(SEG_TOT_WIDTH, SEG_TOT_HEIGHT) + Point(0, gap)
|
||||
// draw indices of each 7-seg display
|
||||
SDL_Point p = Point(SEG_X, SEG_Y) + Point(SEG_TOT_WIDTH, SEG_TOT_HEIGHT) + Point(0, gap)
|
||||
- Point(SEG_TOT_WIDTH / 8 / 2, 0) - Point(CH_WIDTH / 2, CH_HEIGHT / 2);
|
||||
for (int i = 0; i < 8; i ++) {
|
||||
SDL_Texture *t = str2texture(renderer, buf, 0xffffff, BOARD_BG_COLOR);
|
||||
SDL_Rect r = Rect(p0, CH_WIDTH, CH_HEIGHT);
|
||||
SDL_Texture *t = ch2texture(renderer, '0' + i, 0xffffff, BOARD_BG_COLOR);
|
||||
SDL_Rect r = Rect(p, CH_WIDTH, CH_HEIGHT);
|
||||
SDL_RenderCopy(renderer, t, NULL, &r);
|
||||
SDL_DestroyTexture(t);
|
||||
buf[0] ++;
|
||||
p0 = p0 - Point(SEG_TOT_WIDTH / 8, 0);
|
||||
p = p - Point(SEG_TOT_WIDTH / 8, 0);
|
||||
}
|
||||
char *str = "Seven Segment Display";
|
||||
SDL_Texture *t = str2texture(renderer, "Seven Segment Display", 0xffffff, BOARD_BG_COLOR);
|
||||
int w_texture = CH_WIDTH * strlen(str);
|
||||
p0 = Point(SEG_X, SEG_Y) - Point(0, gap) - Point(0, CH_HEIGHT / 2)
|
||||
+ Point(SEG_TOT_WIDTH / 2, 0) - Point(w_texture / 2, 0);
|
||||
SDL_Rect r = Rect(p0, w_texture, CH_HEIGHT);
|
||||
|
||||
// draw the title
|
||||
const char *str = "Seven Segment Display";
|
||||
SDL_Texture *t = str2texture(renderer, str, 0xffffff, BOARD_BG_COLOR);
|
||||
int w = CH_WIDTH * strlen(str);
|
||||
p = Point(SEG_X, SEG_Y) - Point(0, gap) - Point(0, CH_HEIGHT / 2)
|
||||
+ Point(SEG_TOT_WIDTH / 2, 0) - Point(w / 2, 0);
|
||||
SDL_Rect r = Rect(p, w, CH_HEIGHT);
|
||||
SDL_RenderCopy(renderer, t, NULL, &r);
|
||||
SDL_DestroyTexture(t);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue