// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/apps/gapsview/gapsview.cpp
// Function: GLUTMouse(int, int, int, int)
static void
GLUTMouse(int button, int state, int x, int y)
{
  // Invert y coordinate
  y = GLUTwindow_height - y;
  
  // Process mouse button event
  if (state == GLUT_DOWN) {
    // Button is going down
    GLUTmouse_drag_distance_squared = 0;

    // Process thumbwheel
    if ((button == 3) || (button == 4)) {
      R3Point viewing_center_point = world_origin;
      const R3Camera& camera = viewer->Camera();
      R3Plane camera_plane(camera.Origin(), camera.Towards());
      RNScalar signed_distance = R3SignedDistance(camera_plane, viewing_center_point);
      if (signed_distance < 0) viewing_center_point -= (signed_distance - 1) * camera.Towards();
      if (button == 3) viewer->ScaleWorld(viewing_center_point, 1.1);
      else if (button == 4) viewer->ScaleWorld(viewing_center_point, 0.9);
    }
  }
  else { // Button is going up
    // Check for drag
    RNBoolean drag = (GLUTmouse_drag_distance_squared > 10 * 10);
    GLUTmouse_drag_distance_squared = 0;

    // Process button 
    if (button == GLUT_LEFT) {
      // Check for double click  
      // static RNBoolean double_click = FALSE;
      // static RNTime last_mouse_down_time;
      // double_click = (!double_click) && (last_mouse_down_time.Elapsed() < 0.4);
      // last_mouse_down_time.Read();

      // Select position
      if (!drag) {
        int e;
        R3Point p;
        if (Pick(x, y, &p, NULL, &e)) {
          world_origin = p;
          if ((e != GSV_CAMERA_ELEMENT_TYPE) && (e != SFL_CAMERA_ELEMENT_TYPE)) {
            selected_position = p;
          }
        }
      }
    }
  }
  
  // Remember button state 
  int b = (button == GLUT_LEFT_BUTTON) ? 0 : ((button == GLUT_MIDDLE_BUTTON) ? 1 : 2);
  GLUTbutton[b] = (state == GLUT_DOWN) ? 1 : 0;

  // Remember modifiers 
  GLUTmodifiers = glutGetModifiers();

  // Remember mouse position 
  GLUTmouse[0] = x;
  GLUTmouse[1] = y;

  // Redraw
  glutPostRedisplay();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movl %edx, %r12d
movl %esi, %r15d
movl %edi, %r14d
movl 0x191945(%rip), %ebp    # 0x1bea14
subl %ecx, %ebp
testl %esi, %esi
je 0x2d31a
movl 0x197311(%rip), %eax    # 0x1c43f0
andl $0x0, 0x19730a(%rip)    # 0x1c43f0
testl %r14d, %r14d
jne 0x2d408
cmpl $0x64, %eax
jg 0x2d408
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
callq 0xc0dfa
leaq 0x59b644(%rip), %rsi    # 0x5c8750
movq %rbx, %rdi
callq 0xc10e6
movss 0xfe010(%rip), %xmm3    # 0x12b12c
xorps %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x27ec0
movl $0x4100, %edi           # imm = 0x4100
callq 0x27210
movq 0x197054(%rip), %rdi    # 0x1c4190
addq $0x8, %rdi
xorl %esi, %esi
callq 0x7f52a
movl $0xb50, %edi            # imm = 0xB50
callq 0x27220
movl $0x809d, %edi           # imm = 0x809D
callq 0x27220
movss 0xfdfd1(%rip), %xmm0    # 0x12b134
callq 0x270a0
movss 0xfdfc4(%rip), %xmm0    # 0x12b134
callq 0x281f0
pushq $0x5
popq %rdi
callq 0x2d510
movl $0x809d, %edi           # imm = 0x809D
callq 0x27890
movss 0xfdf9d(%rip), %xmm0    # 0x12b12c
callq 0x270a0
movss 0xfdf90(%rip), %xmm0    # 0x12b12c
callq 0x281f0
callq 0x283e0
leaq 0x14(%rsp), %rbx
movq %rbx, (%rsp)
pushq $0x1
popq %rcx
movl %r12d, %edi
movl %ebp, %esi
movl %ecx, %edx
movl $0x1908, %r8d           # imm = 0x1908
movl $0x1401, %r9d           # imm = 0x1401
callq 0x273e0
movzbl 0x3(%rbx), %r13d
testl %r13d, %r13d
je 0x2d408
leal 0x1(%r13), %eax
cmpb $0x11, %al
jb 0x2d408
andl $0xf, %r13d
je 0x2d408
movzwl 0x14(%rsp), %eax
movzbl 0x16(%rsp), %ecx
orl %eax, %ecx
je 0x2d408
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x18(%rsp)
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x40(%rsp)
decl %r13d
leaq 0x50(%rsp), %rsi
movl $0xba2, %edi            # imm = 0xBA2
callq 0x278e0
leaq 0x110(%rsp), %rsi
movl $0xba6, %edi            # imm = 0xBA6
callq 0x27ac0
leaq 0x90(%rsp), %rbx
movl $0xba7, %edi            # imm = 0xBA7
movq %rbx, %rsi
callq 0x27ac0
movl %r12d, 0x10(%rsp)
leaq 0x4c(%rsp), %r12
movq %r12, (%rsp)
pushq $0x1
popq %rcx
movl 0x10(%rsp), %edi
movl %ebp, %esi
movl %ecx, %edx
movl $0x1902, %r8d           # imm = 0x1902
movl $0x1406, %r9d           # imm = 0x1406
callq 0x273e0
cvtss2sd (%r12), %xmm2
leaq 0x28(%rsp), %r8
leaq 0x30(%rsp), %r9
leaq 0x20(%rsp), %r12
movsd 0x18(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
leaq 0x110(%rsp), %rdi
movq %rbx, %rsi
leaq 0x50(%rsp), %rdx
movq %r12, %rcx
callq 0x282d0
movsd (%r12), %xmm0
movsd 0x8(%r12), %xmm1
movsd 0x10(%r12), %xmm2
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
callq 0xc0dfc
leaq 0x60(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0xc10e6
leaq 0x196a67(%rip), %rdi    # 0x1c3d50
movq %r12, %rsi
movl 0x10(%rsp), %r12d
callq 0xc10e6
andl $-0x3, %r13d
cmpl $0x8, %r13d
je 0x2d408
leaq 0x196a5d(%rip), %rdi    # 0x1c3d68
leaq 0x60(%rsp), %rsi
callq 0xc10e6
jmp 0x2d408
andl $0x0, 0x1970cf(%rip)    # 0x1c43f0
leal -0x3(%r14), %eax
cmpl $0x1, %eax
ja 0x2d408
movl %r12d, 0x10(%rsp)
leaq 0x196a16(%rip), %rsi    # 0x1c3d50
leaq 0x90(%rsp), %rdi
callq 0xc0e0c
movq 0x196e42(%rip), %r12    # 0x1c4190
leaq 0x8(%r12), %r13
addq $0x50, %r12
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0xc1f22
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x20(%rsp), %rdx
callq 0xbfa5c
movq %rbx, %rdi
leaq 0x90(%rsp), %rsi
callq 0x8e808
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2d3da
addsd 0xfdcca(%rip), %xmm0    # 0x12b068
movsd %xmm0, 0x18(%rsp)
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xc1f22
leaq 0x20(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movsd 0x18(%rsp), %xmm0
callq 0xc1cb2
leaq 0x90(%rsp), %rdi
movq %r12, %rsi
callq 0xc1494
xorl %eax, %eax
cmpl $0x3, %r14d
sete %al
movq 0x196da6(%rip), %rdi    # 0x1c4190
leaq 0xfdf7f(%rip), %rcx     # 0x12b370
movsd (%rcx,%rax,8), %xmm0
leaq 0x90(%rsp), %rsi
callq 0x7d542
movl 0x10(%rsp), %r12d
xorl %eax, %eax
testl %r15d, %r15d
sete %al
xorl %ecx, %ecx
cmpl $0x1, %r14d
setne %cl
incl %ecx
testl %r14d, %r14d
cmovel %r14d, %ecx
leaq 0x196fcb(%rip), %rdx    # 0x1c43f4
movl %eax, (%rdx,%rcx,4)
callq 0xf82b6
movl %r12d, 0x196fb0(%rip)   # 0x1c43e8
movl %ebp, 0x196fae(%rip)    # 0x1c43ec
callq 0xf3818
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq