1

I would like to implement a simple way to select a section of a tilemap, though I'm stuck on the math of updating the selected area as you drag.

I spent about 12 hours reasearching and trying different things to see if I can get this to work correctly. I didn't think this would be as tough as it is.

My prefered method of implementing the selection, is using four variables: selection_x - X position of the top left corner of the selection selection_y - Y position of the top left corner of the selection selection_w - Width of the selection selection_h - Height of the selection

The scrips tilex() and tiley() return the tile X/Y where the mouse is, calculating for zoom, and view offset.

Here is my attempt:

if(mouse_check_button_pressed(mb_left)) {
  selecting = true;
  selection_start = [tilex(), tiley()];
  selection_stop = [tilex() + 1, tiley() + 1];

  selection_x = tilex();
  selection_y = tiley();
  selection_w = 0;
  selection_h = 0;
} else if(mouse_check_button(mb_left)) {
  var old = selection_x;
  selection_x = tilex() >= selection_x + selection_w ? tilex() : selection_x;
  selection_w = tilex() >= selection_x + selection_w ? tilex() - selection_x : (old - selection_x);

  var old = selection_y;
  selection_y = tiley() >= selection_y + selection_h ? tiley() : selection_y;
  selection_h = tiley() >= selection_y + selection_h ? tiley() - selection_y : (old - selection_y);
  /*
  if(selection_x >= tilex()) {
    var old = selection_x;
    selection_x = tilex();
    selection_w += old - selection_x ;

    selection_stop[0] = tilex() + 1;
    show_debug_message("x 0");
  } else if(selection_x + selection_w <= tilex()) {
    selection_w = tilex() - selection_x + 1;

    selection_start[0] = tilex();
    show_debug_message("x 1");
  }


  if(selection_y >= tiley()) {
    var old = selection_y;
    selection_y = tiley();
    selection_h += old - selection_y;;

    selection_stop[1] = tiley() + 1;
    show_debug_message("y 0");
  } else if(selection_y + selection_h <= tiley()) {
    selection_h = tiley() - selection_y + 1;

    selection_start[1] = tiley();
    show_debug_message("y 1");
  }
  */
  /*
  if(selection_x > tilex() && selection_y > tiley()) {

  } else if(selection_x < tilex() && selection_y < tiley()) {
    selection_x = tilex();
    selection_y = tiley();
    selection_w = selection_x - tilex();
    selection_h = selection_y - tiley();
  }
  */
  /*
  if(selection_start[0] > selection_stop[0] || selection_start[1] < selection_stop[1]) {
    //selection_start = [tilex(), tiley()];
  } else {
    selection_stop = [tilex() + 1, tiley() + 1];
  }
  */
}

Thanks for reading! Hope this can get resolved soon. - Searous <3

Searous
  • 11
  • 4

0 Answers0