<commit_before>#include <algorithm>

#include "table.hpp"
#include "cell.hpp"
#include "random.hpp"


Table::
Table(const TableParams &params):
  CellGroup(nullptr),
  m_rotating(false),
  m_params(params),
  m_first_draw(true)
{
  m_dimensions = m_params.m_wh;
  m_clipped = false;
  m_params.m_cell_count.x() = std::max(1, m_params.m_cell_count.x());
  m_params.m_cell_count.y() = std::max(1, m_params.m_cell_count.y());

  if (m_params.m_text_colors.empty())
    {
      m_params.m_text_colors.push_back(vec4(1.0f, 1.0f, 1.0f, 1.0f));
    }

  if (m_params.m_background_colors.empty())
    {
      m_params.m_background_colors.push_back(vec4(0.0f, 0.0f, 1.0f, 1.0f));
    }

  if (m_params.m_texts.empty())
    {
      m_params.m_texts.push_back("Lonely Text");
    }

  if (m_params.m_images.empty())
    {
      m_params.m_images.push_back(named_image(reference_counted_ptr<const Image>(),
                                              "nullptr"));
    }

}

void
Table::
generate_children_in_group(const reference_counted_ptr<Painter> &painter,
                           CellGroup *g, int &J,
                           const ivec2 &xy,
                           int count_x, int count_y,
                           std::vector<PainterPackedValue<PainterBrush> > &txt,
                           std::vector<PainterPackedValue<PainterBrush> > &bg,
                           std::vector<PainterPackedValue<PainterBrush> > &im)
{
  g->m_bb_min = (vec2(xy) ) * m_cell_sz;
  g->m_bb_max = (vec2(xy) + vec2(count_x, count_y) ) * m_cell_sz;

  if (count_x > m_params.m_max_cell_group_size || count_y > m_params.m_max_cell_group_size)
    {
      int cx1, cx2, cy1, cy2;

      if (count_x > m_params.m_max_cell_group_size)
        {
          cx1 = count_x / 2;
          cx2 = count_x - cx1;
        }
      else
        {
          cx1 = count_x;
          cx2 = 0;
        }

      if (count_y > m_params.m_max_cell_group_size)
        {
          cy1 = count_y / 2;
          cy2 = count_y - cy1;
        }
      else
        {
          cy1 = count_y;
          cy2 = 0;
        }

      CellGroup *p;
      p = FASTUIDRAWnew CellGroup(g);
      generate_children_in_group(painter, p, J, xy, cx1, cy1, txt, bg, im);

      if (cx2 > 0)
        {
          p = FASTUIDRAWnew CellGroup(g);
          generate_children_in_group(painter, p, J, ivec2(xy.x() + cx1, xy.y()),
                                     cx2, cy1, txt, bg, im);
        }

      if (cy2 > 0)
        {
          p = FASTUIDRAWnew CellGroup(g);
          generate_children_in_group(painter, p, J, ivec2(xy.x(), xy.y() + cy1),
                                     cx1, cy2, txt, bg, im);
        }

      if (cx2 > 0 && cy2 > 0)
        {
          p = FASTUIDRAWnew CellGroup(g);
          generate_children_in_group(painter, p, J, ivec2(xy.x() + cx1, xy.y() + cy1),
                                     cx2, cy2, txt, bg, im);
        }
    }
  else
    {
      int x, y;
      vec2 pt;
      for(y = 0, pt.y() = float(xy.y()) * m_cell_sz.y(); y < count_y; ++y, pt.y() += m_cell_sz.y())
        {
          for(x = 0, pt.x() = float(xy.x()) * m_cell_sz.x(); x < count_x; ++x, pt.x() += m_cell_sz.x(), ++J)
            {
              int txtJ, bgJ, imJ;

              txtJ = J % txt.size();
              bgJ = J % bg.size();
              imJ = J % im.size();

              if (!txt[txtJ])
                {
                  PainterBrush brush(m_params.m_text_colors[txtJ]);
                  txt[txtJ] = painter->packed_value_pool().create_packed_value(brush);
                }

              if (!bg[bgJ])
                {
                  PainterBrush brush(m_params.m_background_colors[bgJ]);
                  bg[bgJ] = painter->packed_value_pool().create_packed_value(brush);
                }

              if (!im[imJ])
                {
                  PainterBrush brush;
                  if (m_params.m_images[imJ].first)
                    {
                      brush.image(m_params.m_images[imJ].first);
                    }

                  if (!m_params.m_rect_colors.empty())
                    {
                      brush.pen(m_params.m_rect_colors[imJ]);
                    }
                  im[imJ] = painter->packed_value_pool().create_packed_value(brush);
                }

              CellParams params;
              params.m_glyph_selector = m_params.m_glyph_selector;
              params.m_font = m_params.m_font;
              params.m_background_brush = bg[bgJ];
              params.m_image_brush = im[imJ];
              params.m_text_brush = txt[txtJ];
              params.m_text = m_params.m_texts[J % m_params.m_texts.size()];
              params.m_pixels_per_ms = random_value(m_params.m_min_speed, m_params.m_max_speed) / 1000.0f;
              params.m_degrees_per_s = (int)random_value(m_params.m_min_degrees_per_s, m_params.m_max_degrees_per_s);
              params.m_text_render = m_params.m_text_render;
              params.m_pixel_size = m_params.m_pixel_size;
              params.m_size = m_cell_sz;
              params.m_table_pos = ivec2(x, y) + xy;
              if (m_params.m_draw_image_name)
                {
                  params.m_image_name = m_params.m_images[imJ].second;
                }
              else
                {
                  params.m_image_name = "";
                }
              params.m_line_brush = m_line_brush;
              params.m_state = m_params.m_cell_state;
              params.m_timer_based_animation = m_params.m_timer_based_animation;

              Cell *cell;
              cell = FASTUIDRAWnew Cell(g, params);
              cell->m_parent_matrix_this = float3x3(float2x2(), pt);
            }
        }
    }
}


void
Table::
paint_pre_children(const reference_counted_ptr<Painter> &painter)
{
  if (m_first_draw)
    {
      vec2 cell_loc;
      int x, y, J;
      std::vector<PainterPackedValue<PainterBrush> > txt, bg, im;

      txt.resize(m_params.m_text_colors.size());
      bg.resize(m_params.m_background_colors.size());
      im.resize(m_params.m_images.size());
      m_cell_sz = m_dimensions / vec2(m_params.m_cell_count);

      m_params.m_cell_state->m_path << vec2(0.0f, 0.0f)
                                    << vec2(m_cell_sz.x(), 0.0f)
                                    << vec2(m_cell_sz.x(), m_cell_sz.y())<commit_msg>demos/painter_cells: make rect-color modulation and image selection independent<commit_after>#include <algorithm>

#include "table.hpp"
#include "cell.hpp"
#include "random.hpp"


Table::
Table(const TableParams &params):
  CellGroup(nullptr),
  m_rotating(false),
  m_params(params),
  m_first_draw(true)
{
  m_dimensions = m_params.m_wh;
  m_clipped = false;
  m_params.m_cell_count.x() = std::max(1, m_params.m_cell_count.x());
  m_params.m_cell_count.y() = std::max(1, m_params.m_cell_count.y());

  if (m_params.m_text_colors.empty())
    {
      m_params.m_text_colors.push_back(vec4(1.0f, 1.0f, 1.0f, 1.0f));
    }

  if (m_params.m_background_colors.empty())
    {
      m_params.m_background_colors.push_back(vec4(0.0f, 0.0f, 1.0f, 1.0f));
    }

  if (m_params.m_texts.empty())
    {
      m_params.m_texts.push_back("Lonely Text");
    }

  if (m_params.m_images.empty())
    {
      m_params.m_images.push_back(named_image(reference_counted_ptr<const Image>(),
                                              "nullptr"));
    }

  if (m_params.m_rect_colors.empty())
    {
      m_params.m_rect_colors.push_back(vec4(1.0f, 1.0f, 1.0f, 1.0f));
    }

}

void
Table::
generate_children_in_group(const reference_counted_ptr<Painter> &painter,
                           CellGroup *g, int &J,
                           const ivec2 &xy,
                           int count_x, int count_y,
                           std::vector<PainterPackedValue<PainterBrush> > &txt,
                           std::vector<PainterPackedValue<PainterBrush> > &bg,
                           std::vector<PainterPackedValue<PainterBrush> > &im)
{
  g->m_bb_min = (vec2(xy) ) * m_cell_sz;
  g->m_bb_max = (vec2(xy) + vec2(count_x, count_y) ) * m_cell_sz;

  if (count_x > m_params.m_max_cell_group_size || count_y > m_params.m_max_cell_group_size)
    {
      int cx1, cx2, cy1, cy2;

      if (count_x > m_params.m_max_cell_group_size)
        {
          cx1 = count_x / 2;
          cx2 = count_x - cx1;
        }
      else
        {
          cx1 = count_x;
          cx2 = 0;
        }

      if (count_y > m_params.m_max_cell_group_size)
        {
          cy1 = count_y / 2;
          cy2 = count_y - cy1;
        }
      else
        {
          cy1 = count_y;
          cy2 = 0;
        }

      CellGroup *p;
      p = FASTUIDRAWnew CellGroup(g);
      generate_children_in_group(painter, p, J, xy, cx1, cy1, txt, bg, im);

      if (cx2 > 0)
        {
          p = FASTUIDRAWnew CellGroup(g);
          generate_children_in_group(painter, p, J, ivec2(xy.x() + cx1, xy.y()),
                                     cx2, cy1, txt, bg, im);
        }

      if (cy2 > 0)
        {
          p = FASTUIDRAWnew CellGroup(g);
          generate_children_in_group(painter, p, J, ivec2(xy.x(), xy.y() + cy1),
                                     cx1, cy2, txt, bg, im);
        }

      if (cx2 > 0 && cy2 > 0)
        {
          p = FASTUIDRAWnew CellGroup(g);
          generate_children_in_group(painter, p, J, ivec2(xy.x() + cx1, xy.y() + cy1),
                                     cx2, cy2, txt, bg, im);
        }
    }
  else
    {
      int x, y;
      vec2 pt;
      for(y = 0, pt.y() = float(xy.y()) * m_cell_sz.y(); y < count_y; ++y, pt.y() += m_cell_sz.y())
        {
          for(x = 0, pt.x() = float(xy.x()) * m_cell_sz.x(); x < count_x; ++x, pt.x() += m_cell_sz.x(), ++J)
            {
              int txtJ, bgJ, imJ;

              txtJ = J % txt.size();
              bgJ = J % bg.size();
              imJ = J % im.size();

              if (!txt[txtJ])
                {
                  PainterBrush brush(m_params.m_text_colors[txtJ]);
                  txt[txtJ] = painter->packed_value_pool().create_packed_value(brush);
                }

              if (!bg[bgJ])
                {
                  PainterBrush brush(m_params.m_background_colors[bgJ]);
                  bg[bgJ] = painter->packed_value_pool().create_packed_value(brush);
                }

              if (!im[imJ])
                {
                  int image, color;

                  image = imJ % m_params.m_images.size();
                  color = imJ % m_params.m_rect_colors.size();

                  PainterBrush brush;
                  if (m_params.m_images[image].first)
                    {
                      brush.image(m_params.m_images[image].first);
                    }

                  brush.pen(m_params.m_rect_colors[color]);
                  im[imJ] = painter->packed_value_pool().create_packed_value(brush);
                }

              CellParams params;
              params.m_glyph_selector = m_params.m_glyph_selector;
              params.m_font = m_params.m_font;
              params.m_background_brush = bg[bgJ];
              params.m_image_brush = im[imJ];
              params.m_text_brush = txt[txtJ];
              params.m_text = m_params.m_texts[J % m_params.m_texts.size()];
              params.m_pixels_per_ms = random_value(m_params.m_min_speed, m_params.m_max_speed) / 1000.0f;
              params.m_degrees_per_s = (int)random_value(m_params.m_min_degrees_per_s, m_params.m_max_degrees_per_s);
              params.m_text_render = m_params.m_text_render;
              params.m_pixel_size = m_params.m_pixel_size;
              params.m_size = m_cell_sz;
              params.m_table_pos = ivec2(x, y) + xy;
              if (m_params.m_draw_image_name)
                {
                  params.m_image_name = m_params.m_images[imJ].second;
                }
              else
                {
                  params.m_image_name = "";
                }
              params.m_line_brush = m_line_brush;
              params.m_state = m_params.m_cell_state;
              params.m_timer_based_animation = m_params.m_timer_based_animation;

              Cell *cell;
              cell = FASTUIDRAWnew Cell(g, params);
              cell->m_parent_matrix_this = float3x3(float2x2(), pt);
            }
        }
    }
}


void
Table::
paint_pre_children(const reference_counted_ptr<Painter> &painter)
{
  if (m_first_draw)
    {
      vec2 cell_loc;
      int x, y, J;
      std::vector<PainterPackedValue<PainterBrush> > txt, bg, im;

      txt.resize(m_params.m_text_colors.size());
      bg.resize(m_params.m_background_colors.size());
      im.resize(m_params.m_images.size() * m_params.m_rect_colors.size());
      m_cell_sz = m_dimensions / vec2(m_params.m_cell_count);

      m_params.m_cell_state->m_path << vec2(0.0f, 0.0f)
                                    << vec2(m_cell_sz.x(), 0.0f)
                                    << vec2(m_cell_sz.x(), m_cell_sz.y())
{'fraction_non_alphanumeric': 0.12210153300978353, 'fraction_numerical': 0.014405669825129034, 'mean_word_length': 1.647226753670473, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4865835', 'n_tokens_mistral': 4784, 'n_tokens_neox': 4552, 'n_words': 680}