~l3kn/quad

35252b0daab015403d8b85b715441c14f6b5163c — Leon Rische 6 months ago 010bf2a
Dynamic selector position
1 files changed, 95 insertions(+), 70 deletions(-)

M quad.c
M quad.c => quad.c +95 -70
@@ 21,46 21,27 @@ SDL_Texture* grammarTexture = NULL;

double zoomFactor = 1.0;
int currentRule = 0;
int currentQuad = 0;

Node selection;
Rule rules[NRULES];
int colors[2] = { COLOR_BLACK, COLOR_WHITE };

bool outline = true;
bool quit = false;
bool zooming = false;
int limit = 1;

void expandRule(SDL_Renderer* renderer, int x, int y, int s, Rule* rule);
void expandNode(SDL_Renderer* renderer, int x, int y, int s, Node node) {
  if (node.type == RULE) {
    expandRule(renderer, x, y, s, &rules[node.index]);
  } else if (node.type == PATTERN) {
    drawSquare(renderer, x, y, s, patterns[node.index]);
  }
}

void expandRule(SDL_Renderer* renderer, int x, int y, int s, Rule* rule) {
  int h = s / 2;
  if (s > limit) {
    expandNode(renderer, x, y, h, rule->quads[0]);
    expandNode(renderer, x + h, y, h, rule->quads[1]);
    expandNode(renderer, x, y + h, h, rule->quads[2]);
    expandNode(renderer, x + h, y + h, h, rule->quads[3]);
  } else if (rule->fallback.type == PATTERN) {
    expandNode(renderer, x, y, s, rule->fallback);
  }
}
bool selector = false;
int selectorX = PADDING;
int selectorY = PADDING;

void saveBMP() {
  SDL_Surface* surface = SDL_GetWindowSurface(window);
  SDL_RenderReadPixels(mainRenderer,
                       NULL,
                       SDL_PIXELFORMAT_ARGB8888,
                       surface->pixels,
                       surface->pitch);
  SDL_SaveBMP(surface, "quad-render.bmp");
  SDL_FreeSurface(surface);
  /* SDL_Surface* surface = SDL_GetWindowSurface(window); */
  /* SDL_RenderReadPixels(grammarRenderer, */
  /*                      NULL, */
  /*                      SDL_PIXELFORMAT_ARGB8888, */
                       /* grammarSurface->pixels, */
                       /* grammarSurface->pitch); */
  SDL_SaveBMP(grammarSurface, "quad-render.bmp");
  /* SDL_FreeSurface(surface); */
}

void setColor(SDL_Renderer* renderer, int color, int alpha) {


@@ 87,7 68,7 @@ void draw() {
  setColor(mainRenderer, colors[1], SDL_ALPHA_OPAQUE);

  drawSquare(mainRenderer, CANVAS_X-1, CANVAS_Y-1, SIZE+1, BLACK);
  drawMenu(mainRenderer, &patterns, &rules);
  drawMenu(mainRenderer, PADDING, PADDING, &patterns, &rules);

  SDL_Rect src, target;
  src.x = 0;


@@ 102,6 83,10 @@ void draw() {

  SDL_RenderCopy(mainRenderer, grammarTexture, &src, &target);
  /* SDL_BlitScaled(grammarSurface, &src, winSurface, &target); */

  if (selector) {
    drawSelector(mainRenderer, selectorX, selectorY, &patterns);
  }
  SDL_RenderPresent(mainRenderer);
}



@@ 215,48 200,88 @@ void handleKey(SDL_Event* event) {
}
}


void handleMouse(SDL_Event* event) {
  int tx = (event->motion.x - PADDING) / TILE_SIZE;
  int ty = (event->motion.y - PADDING) / TILE_SIZE;

  if (tx >= 0 && tx < NPATTERNS && \
      ty >= 0 && ty < HPATTERNS) {
    selection.type = PATTERN;
    selection.index = tx + (10 * ty);
  } else if (tx >= 0 && tx < MENU_WIDTH && \
             ty >= (HPATTERNS + 1) && ty < MENU_HEIGHT) {
    ty -= (HPATTERNS + 1);

    int rule = WRULES * (ty / 3) + (tx / 6);
    int rx = tx % 6;
    int ry = ty % 3;
    printf("%d,%d\n", rx, ry);

    if (rx == 0 && ry == 0) {
      selection.type = RULE;
      selection.index = rule;
    } else if (rx == 0 && ry == 1 && selection.type == PATTERN) {
      rules[rule].fallback = selection;
      renderRule(grammarRenderer);
      draw();
    } else if (rx == 2 && ry == 0) {
      rules[rule].quads[0] = selection;
      renderRule(grammarRenderer);
      draw();
    } else if (rx == 3 && ry == 0) {
      rules[rule].quads[1] = selection;
      renderRule(grammarRenderer);
      draw();
    } else if (rx == 2 && ry == 1) {
      rules[rule].quads[2] = selection;
      renderRule(grammarRenderer);
  int x = event->motion.x;
  int y = event->motion.y;

  if (x >= CANVAS_X && x < CANVAS_X + SIZE && \
      y >= CANVAS_Y && y < CANVAS_Y + SIZE) {
    int sx = CANVAS_X + TILE_SIZE;
    int sy = CANVAS_Y + TILE_SIZE;
    int sQuad = 0;

    if ((x - CANVAS_X) > HALF) {
      sx += HALF;
      sQuad += 1;
    }
    if ((y - CANVAS_Y) > HALF) {
      sy += HALF;
      sQuad += 2;
    }

    if (selector && sQuad == currentQuad) {
      if (x >= selectorX && x < selectorX + (WPATTERNS * TILE_SIZE) && \
          y >= selectorY && y < selectorY + (HPATTERNS * TILE_SIZE)) {
        int tx = (x - selectorX) / TILE_SIZE;
        int ty = (y - selectorY) / TILE_SIZE;
        selection.type = PATTERN;
        selection.index = tx + (WPATTERNS * ty);
        rules[currentRule].quads[sQuad] = selection;
        renderRule(grammarRenderer);
      } else if (x >= selectorX && x < selectorX + (WRULES * TILE_SIZE) && \
                 y >= selectorY + (HPATTERNS * TILE_SIZE) && \
                 y < selectorY + ((HPATTERNS + HRULES) * TILE_SIZE)) {
        int tx = (x - selectorX) / TILE_SIZE;
        int ty = (y - selectorY - (HPATTERNS * TILE_SIZE)) / TILE_SIZE;
        selection.type = RULE;
        selection.index = tx + (WRULES * ty);
        rules[currentRule].quads[sQuad] = selection;
        renderRule(grammarRenderer);
      }
      selector = false;
      draw();
    } else if (rx == 3 && ry == 1) {
      rules[rule].quads[3] = selection;
      renderRule(grammarRenderer);
    } else {
      selector = true;
      currentQuad = sQuad;
      selectorX = sx;
      selectorY = sy;
      draw();
    }
  } else {
    int tx = (x - PADDING) / TILE_SIZE;
    int ty = (y - PADDING) / TILE_SIZE;

    if (tx >= 0 && tx < WPATTERNS && \
        ty >= 0 && ty < HPATTERNS) {
      selection.type = PATTERN;
      selection.index = tx + (WPATTERNS * ty);
    } else if (tx >= 0 && tx < WRULES && \
               ty >= HPATTERNS && ty < (HPATTERNS + HRULES)) {
      ty -= HPATTERNS;
      selection.type = RULE;
      selection.index = tx + (WRULES * ty);
    } else if (tx >= 0 && tx < MENU_WIDTH && \
               ty >= (HPATTERNS + HRULES + 1) && ty < MENU_HEIGHT) {
      ty -= (HPATTERNS + HRULES + 1);

      int rule = WEDITRULES * (ty / 3) + (tx / 6);
      int rx = tx % 6;
      int ry = ty % 3;

      if (rx == 0 && ry == 0) {
        selection.type = RULE;
        selection.index = rule;
      } else if (rx == 0 && ry == 1 && selection.type == PATTERN) {
        rules[rule].fallback = selection;
        renderRule(grammarRenderer);
        draw();
      } else if (rx >= 2 && rx <= 3 && ry >= 0 && ry <= 1) {
        int quad = ry * 2 + (rx - 2);
        rules[rule].quads[quad] = selection;
        renderRule(grammarRenderer);
        draw();
      }
    }
  }
}