Subversion Repositories DIN Is Noise

Rev

Rev 2117 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
964 jag 1
/*
2
* textboard.cc
2302 jag 3
* DIN Is Noise is copyright (c) 2006-2025 Jagannathan Sampath
1713 jag 4
* DIN Is Noise is released under GNU Public License 2.0
1479 jag 5
* For more information, please visit https://dinisnoise.org/
964 jag 6
*/
7
 
8
 
9
#include "textboard.h"
10
#include "dingl.h"
11
#include "font.h"
12
#include "basic_editor.h"
13
#include "mondrian.h"
1923 jag 14
#include "console.h"
964 jag 15
 
16
#include <iostream>
17
using namespace std;
18
 
19
extern viewport view;
20
extern int line_height;
21
 
22
void textboard::draw (int shapeform) {
2116 jag 23
 
964 jag 24
  glMatrixMode (GL_PROJECTION);
25
    glPushMatrix ();
26
      glLoadIdentity ();
27
      glOrtho (0, view.xmax, 0, view.ymax, -1, 1);
2116 jag 28
 
964 jag 29
  glMatrixMode (GL_MODELVIEW);
30
  glLoadIdentity ();
2116 jag 31
 
964 jag 32
  for (list<text>::iterator i = texts.begin (), j = texts.end (); i != j; ++i) {
33
    text& t = *i;
2116 jag 34
    if (t.type == text::floating) {
2005 jag 35
      glColor3f (t.r, t.g, t.b);
2116 jag 36
      draw_string (t.txt, t.vx, t.vy);
1923 jag 37
    } else if (t.type == text::vline) {
38
      if (shapeform == 0) {
39
        draw_line (t.vx, 0, t.vx, view.ymax);
40
        glColor3f (t.r, t.g, t.b);
41
        draw_string (t.txt, t.vx, t.vy);
42
      }
43
    } else {
964 jag 44
      draw_line (0, t.vy, view.xmax, t.vy);
45
      glColor3f (t.r, t.g, t.b);
46
      draw_string (t.txt, t.vx, t.vy);
1923 jag 47
    }
964 jag 48
  }
2116 jag 49
 
50
  // glPopMatrix ();
51
 
964 jag 52
  glMatrixMode (GL_PROJECTION);
53
  glPopMatrix ();
54
  glMatrixMode (GL_MODELVIEW);
55
}
56
 
57
void textboard::draw_line (int x1, int y1, int x2, int y2) {
2116 jag 58
 
1923 jag 59
  /*static int pts [4] = {0};
964 jag 60
  pts[0]=x1;pts[1]=y1;
61
  pts[2]=x2;pts[3]=y2;
62
  glVertexPointer (2, GL_INT, 0, pts);
1923 jag 63
  glDrawArrays (GL_LINES, 0, 2);*/
2116 jag 64
 
1923 jag 65
  glColor3f (0.3f, 0.3f, 0.3f);
66
  glBegin (GL_LINES);
67
    glVertex2i (x1, y1);
68
    glVertex2i (x2, y2);
69
  glEnd ();
2116 jag 70
 
964 jag 71
}
72
 
73
void textboard::clear () {
74
  for (list<text>::iterator i = texts.begin (), j = texts.end (); i != j;) {
75
    text& ti = *i;
2116 jag 76
    if (ti.state == text::once) {
964 jag 77
      i = texts.erase (i);
78
      j = texts.end ();
79
    } else ++i;
80
  }
81
}
82
 
987 jag 83
void textboard::refresh (basic_editor* b, float dwx, float dwy, float dvx, float dvy) {
964 jag 84
  static const int spc = 3;
969 jag 85
  extern viewport view;
86
  float wx, wy;
964 jag 87
  for (list<text>::iterator i = texts.begin (), j = texts.end (); i != j; ++i) {
88
    text& ti = *i;
987 jag 89
    b->obj2win (ti.wx, ti.wy, wx, wy); wx += dwx; wy += dwy;
90
    win2view (wx, wy, ti.vx, ti.vy, b->win, view); ti.vx += dvx; ti.vy += dvy;
964 jag 91
    if (ti.type == text::hline) ti.vx = spc; else if (ti.type == text::vline) ti.vy = line_height;
92
  }
93
}
94
 
95
void textboard::refresh (mondrian* b) {
96
  for (list<text>::iterator i = texts.begin (), j = texts.end (); i != j; ++i) {
97
    text& ti = *i;
98
    float wx, wy; b->obj2win (ti.wx, ti.wy, wx, wy);
99
    win2view (wx, wy, ti.vx, ti.vy, b->win, view);
100
  }
101
}
102
 
103
void textboard::load (ifstream& file) {
104
  string ignore, txt;
105
  float r, g, b, wx, wy;
2116 jag 106
  int type, ntexts, state;
964 jag 107
  file >> ignore >> ntexts;
108
  for (int i = 0; i < ntexts; ++i) {
2116 jag 109
    file >> txt >> wx >> wy >> r >> g >> b >> type >> state;
110
    add (text (txt, wx, wy, r, g, b, state, type));
964 jag 111
  }
112
}
113
 
114
void textboard::save (ofstream& file) {
115
  static const char spc = ' ';
116
  int ntexts = texts.size ();
117
  file << "num_labels " << ntexts << endl;
118
  for (list<text>::iterator i = texts.begin (), j = texts.end (); i != j; ++i) {
119
    text& ti = *i;
2116 jag 120
    if (ti.type != text::floating) {
121
      file << ti.txt << spc << ti.wx << spc << ti.wy << spc << ti.r << spc << ti.g << spc << ti.b << spc << ti.type << spc << ti.state << endl;
964 jag 122
    }
123
  }
124
}
2116 jag 125
 
126
void textboard::delallguides (int thistype) {
127
  for (list<text>::iterator i = texts.begin (), j = texts.end (); i != j;) {
128
    text& ti = *i;
129
    if (ti.type == thistype && ti.state == text::session) {
130
      i = texts.erase (i);
131
      j = texts.end ();
132
    } else ++i;
133
  }
134
}
135
 
136
void textboard::delnearestguide (int type, int vx, int vy) {
137
  for (list<text>::iterator i = texts.begin (), j = texts.end (); i != j;) {
138
    text& ti = *i;
139
    if (ti.state == text::session) {
140
      int vat [2] = {ti.vy, ti.vx};
141
      int vai [2] = {vy, vx};
142
      float d = vat[type] -  vai[type];
143
      float d2 = d*d;
2117 jag 144
      static const int mind = 10;
145
      static const int mind2 = mind * mind;
2116 jag 146
      if (d2 > mind2)
147
        ++i;
148
      else {
149
        i = texts.erase (i);
150
        j = texts.end ();
151
      }
152
    } else ++i;
153
  }
154
}