~l3kn/quad

525a8b32b4184d42a2e0f9fdfada8cf3feff745b — Leon Rische 6 months ago 0df581a
Rewrite color/rule selector & print rules
1 files changed, 59 insertions(+), 67 deletions(-)

M quad.c
M quad.c => quad.c +59 -67
@@ 15,9 15,8 @@ SDL_Window* window = NULL;
SDL_Renderer* windowRenderer = NULL;

int currentRule = 0;
int currentQuad = 0;
Node* selNode = NULL;

Node selection;
Rule rules[NRULES];

bool quit = false;


@@ 154,75 153,67 @@ void handleMouse(SDL_Event* event) {
  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;
    int sy = CANVAS_Y;
    int sQuad = 0;

    if ((x - CANVAS_X) > HALF) {
      sx += HALF;
      sQuad += 1;
    }
    if ((y - CANVAS_Y) > HALF) {
      sy += HALF;
      sQuad += 2;
  if (selector) {
    selector = false;
    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;
      selNode->type = PATTERN;
      selNode->index = tx + WPATTERNS * ty;
    } 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;
      selNode->type = RULE;
      selNode->index = tx + (WRULES * ty);
    }

    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 {
    draw(true);
  } else if (x >= MENU_X && x < MENU_X + MENU_WIDTH * TILE_SIZE && \
             y >= MENU_Y && y < MENU_Y + MENU_HEIGHT * TILE_SIZE) {
    int mx = (x - MENU_X);
    int my = (y - MENU_Y);
    int tx = mx / TILE_SIZE;
    int ty = my / TILE_SIZE;
    int index = (ty / HEDIT) * 2 + (tx / WEDIT);
    int rx = tx % WEDIT;
    int ry = ty % HEDIT;

    if (rx == 0 && ry == 0) {
      currentRule = index;
      draw(true);
    } else if (rx == 0 && ry == 1) {
      Rule* rule = &rules[index];
      rule->fallback.index = !rule->fallback.index;
      draw(true);
    } else if (rx >= 1 && rx < 3 && ry >= 0 && ry < 2) {
      int quad = ry * 2 + (rx - 1);
      selNode = &(rules[index].quads[quad]);
      selector = true;
      currentQuad = sQuad;
      selectorX = sx;
      selectorY = sy;
      draw();
      selectorX = MENU_X + mx - (mx % TILE_SIZE);
      selectorY = MENU_Y + my - (my % TILE_SIZE);
      draw(true);
    }
  }
}

void printNode(Node* node) {
  if (node->type == RULE) {
    printf("R%02d", node->index);
  } else {
    int tx = (x - PADDING) / TILE_SIZE;
    int ty = (y - PADDING) / TILE_SIZE;

    /* 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(); */
    /* } */
    /* } */
    printf("P%02d", node->index);
  }
}

void printRules() {
  for (int i = 0; i < NRULES; ++i) {
    Rule* rule = &rules[i];
    for (int j = 0; j < 4; ++j) {
      printNode(&(rule->quads[j]));
    }
    printNode(&(rule->fallback));
    printf("\n");
  }
}



@@ 266,6 257,7 @@ int main(int argc, char* argv[]) {
    SDL_Delay(1000 / 60);
  }

  printRules();

  SDL_DestroyRenderer(windowRenderer);
  SDL_DestroyWindow(window);