curvedraw
annotate src/widgets.cc @ 14:b625f0575d66
point snapping
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 17 Dec 2015 16:41:42 +0200 |
parents | 8e524989c904 |
children | 2b7ae76c173f |
rev | line source |
---|---|
nuclear@0 | 1 #include <stdio.h> |
nuclear@0 | 2 #include <stdlib.h> |
nuclear@0 | 3 #include <string.h> |
nuclear@0 | 4 #include <stdarg.h> |
nuclear@0 | 5 #include <drawtext.h> |
nuclear@0 | 6 #include "opengl.h" |
nuclear@0 | 7 #include "widgets.h" |
nuclear@0 | 8 |
nuclear@0 | 9 static dtx_font *font; |
nuclear@0 | 10 |
nuclear@0 | 11 static void init_font() |
nuclear@0 | 12 { |
nuclear@0 | 13 if(font) return; |
nuclear@0 | 14 |
nuclear@1 | 15 if(!(font = dtx_open_font("data/droid_sans.ttf", 24))) { |
nuclear@0 | 16 fprintf(stderr, "failed to load font\n"); |
nuclear@0 | 17 abort(); |
nuclear@0 | 18 } |
nuclear@0 | 19 } |
nuclear@0 | 20 |
nuclear@0 | 21 Widget::Widget() |
nuclear@0 | 22 { |
nuclear@0 | 23 text = 0; |
nuclear@0 | 24 } |
nuclear@0 | 25 |
nuclear@0 | 26 Widget::~Widget() |
nuclear@0 | 27 { |
nuclear@0 | 28 delete [] text; |
nuclear@0 | 29 } |
nuclear@0 | 30 |
nuclear@0 | 31 void Widget::set_position(const Vector2 &p) |
nuclear@0 | 32 { |
nuclear@0 | 33 pos = p; |
nuclear@0 | 34 } |
nuclear@0 | 35 |
nuclear@0 | 36 const Vector2 &Widget::get_position() const |
nuclear@0 | 37 { |
nuclear@0 | 38 return pos; |
nuclear@0 | 39 } |
nuclear@0 | 40 |
nuclear@0 | 41 void Widget::set_text(const char *str) |
nuclear@0 | 42 { |
nuclear@1 | 43 char *newtext = new char[strlen(str) + 1]; |
nuclear@1 | 44 strcpy(newtext, str); |
nuclear@1 | 45 |
nuclear@1 | 46 delete [] text; |
nuclear@1 | 47 text = newtext; |
nuclear@0 | 48 } |
nuclear@0 | 49 |
nuclear@0 | 50 void Widget::set_textf(const char *str, ...) |
nuclear@0 | 51 { |
nuclear@0 | 52 va_list ap; |
nuclear@0 | 53 int sz = strlen(str) * 4; |
nuclear@1 | 54 char *buf = (char*)alloca(sz + 1); |
nuclear@0 | 55 |
nuclear@0 | 56 va_start(ap, str); |
nuclear@1 | 57 vsnprintf(buf, sz, str, ap); |
nuclear@0 | 58 va_end(ap); |
nuclear@0 | 59 |
nuclear@0 | 60 set_text(buf); |
nuclear@0 | 61 } |
nuclear@0 | 62 |
nuclear@0 | 63 const char *Widget::get_text() const |
nuclear@0 | 64 { |
nuclear@0 | 65 return text; |
nuclear@0 | 66 } |
nuclear@0 | 67 |
nuclear@0 | 68 // ---- label ---- |
nuclear@0 | 69 |
nuclear@0 | 70 void Label::draw() const |
nuclear@0 | 71 { |
nuclear@0 | 72 init_font(); |
nuclear@0 | 73 |
nuclear@0 | 74 glMatrixMode(GL_MODELVIEW); |
nuclear@0 | 75 glPushMatrix(); |
nuclear@0 | 76 glTranslatef(pos.x, pos.y, 0); |
nuclear@1 | 77 glScalef(0.003, 0.003, 1); |
nuclear@0 | 78 |
nuclear@1 | 79 glColor4f(1, 1, 1, 1); |
nuclear@0 | 80 dtx_string(text); |
nuclear@0 | 81 dtx_flush(); |
nuclear@0 | 82 |
nuclear@0 | 83 glPopMatrix(); |
nuclear@0 | 84 } |