// File: /workspace/github_cpp_makefile_star2_O1/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, %r13d
movl %esi, %ebx
movl %edi, %r14d
movl 0x1a970b(%rip), %ebp    # 0x1d7a04
subl %ecx, %ebp
testl %esi, %esi
je 0x2e53a
movl 0x1af0d7(%rip), %eax    # 0x1dd3e0
movl $0x0, 0x1af0cd(%rip)    # 0x1dd3e0
testl %r14d, %r14d
jne 0x2e66f
cmpl $0x64, %eax
jg 0x2e66f
movl %ebx, 0x10(%rsp)
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
callq 0xd5e2c
leaq 0x5b3403(%rip), %rsi    # 0x5e1740
movq %rbx, %rdi
callq 0xd619c
movss 0x11fde7(%rip), %xmm3   # 0x14e134
xorps %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x27eb0
movl $0x4100, %edi           # imm = 0x4100
callq 0x271f0
movq 0x1aee13(%rip), %rdi    # 0x1dd180
addq $0x8, %rdi
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x8bc7c
movl $0xb50, %edi            # imm = 0xB50
callq 0x27200
movl $0x809d, %edi           # imm = 0x809D
callq 0x27200
movss 0x11fda6(%rip), %xmm0   # 0x14e13c
callq 0x270a0
movss 0x11fd99(%rip), %xmm0   # 0x14e13c
callq 0x281e0
movl $0x5, %edi
callq 0x2e77a
movl $0x809d, %edi           # imm = 0x809D
callq 0x27880
movss 0x11fd70(%rip), %xmm0   # 0x14e134
callq 0x270a0
movss 0x11fd63(%rip), %xmm0   # 0x14e134
callq 0x281e0
callq 0x283b0
leaq 0x14(%rsp), %r15
movq %r15, (%rsp)
movl %r13d, %edi
movl %ebp, %esi
movl $0x1, %edx
movl $0x1, %ecx
movl $0x1908, %r8d           # imm = 0x1908
movl $0x1401, %r9d           # imm = 0x1401
callq 0x273d0
movzbl 0x3(%r15), %r12d
movb $0x1, %al
testl %r12d, %r12d
je 0x2e623
leal 0x1(%r12), %ecx
cmpb $0x11, %cl
jb 0x2e621
andl $0xf, %r12d
je 0x2e621
movzbl 0x14(%rsp), %ecx
movzbl 0x15(%rsp), %edx
movzbl 0x16(%rsp), %esi
shll $0x8, %edx
orl %ecx, %edx
orl %esi, %edx
je 0x2e621
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x20(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x18(%rsp)
decl %r12d
leaq 0x60(%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
leaq 0x2c(%rsp), %r15
movq %r15, (%rsp)
movl %r13d, %edi
movl %ebp, %esi
movl $0x1, %edx
movl $0x1, %ecx
movl $0x1902, %r8d           # imm = 0x1902
movl $0x1406, %r9d           # imm = 0x1406
callq 0x273d0
cvtss2sd (%r15), %xmm2
leaq 0x38(%rsp), %r8
leaq 0x40(%rsp), %r9
leaq 0x30(%rsp), %r15
movsd 0x20(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
leaq 0x110(%rsp), %rdi
movq %rbx, %rsi
leaq 0x60(%rsp), %rdx
movq %r15, %rcx
callq 0x282c0
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
movsd 0x10(%r15), %xmm2
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
callq 0xd5e2e
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0xd619c
andl $-0x3, %r12d
cmpl $0x8, %r12d
sete %bl
xorl %eax, %eax
jmp 0x2e623
movl $0x0, 0x1aee9c(%rip)    # 0x1dd3e0
leal -0x3(%r14), %eax
cmpl $0x1, %eax
ja 0x2e66f
movl %ebx, 0x10(%rsp)
movl %r13d, 0x20(%rsp)
leaq 0x1ae7df(%rip), %rsi    # 0x1dcd40
leaq 0x90(%rsp), %rdi
callq 0xd5e3e
movq 0x1aec0b(%rip), %r12    # 0x1dd180
leaq 0x8(%r12), %r13
addq $0x50, %r12
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xd755c
leaq 0x110(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0xd49ae
movq %r15, %rdi
leaq 0x90(%rsp), %rsi
callq 0x9d73b
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2e602
addsd 0x11faaa(%rip), %xmm0   # 0x14e070
movsd %xmm0, 0x18(%rsp)
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xd755c
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movsd 0x18(%rsp), %xmm0
callq 0xd7296
leaq 0x90(%rsp), %rdi
movq %r15, %rsi
callq 0xd679c
cmpl $0x3, %r14d
movl 0x20(%rsp), %r13d
movl 0x10(%rsp), %ebx
je 0x2e653
cmpl $0x4, %r14d
jne 0x2e66f
movsd 0x11fa61(%rip), %xmm0   # 0x14e080
jmp 0x2e65b
xorl %ebx, %ebx
testb %al, %al
jne 0x2e64d
leaq 0x1ae712(%rip), %rdi    # 0x1dcd40
leaq 0x48(%rsp), %rsi
callq 0xd619c
testb %bl, %bl
jne 0x2e64d
leaq 0x1ae715(%rip), %rdi    # 0x1dcd58
leaq 0x48(%rsp), %rsi
callq 0xd619c
movl 0x10(%rsp), %ebx
jmp 0x2e66f
movsd 0x11fa1d(%rip), %xmm0   # 0x14e078
movq 0x1aeb1e(%rip), %rdi    # 0x1dd180
leaq 0x90(%rsp), %rsi
callq 0x89aa8
xorl %eax, %eax
testl %ebx, %ebx
sete %al
xorl %ecx, %ecx
cmpl $0x1, %r14d
setne %cl
incl %ecx
testl %r14d, %r14d
cmovel %r14d, %ecx
leaq 0x1aed55(%rip), %rdx    # 0x1dd3e4
movl %eax, (%rdx,%rcx,4)
callq 0x116ed4
movl %r13d, 0x1aed3a(%rip)   # 0x1dd3d8
movl %ebp, 0x1aed38(%rip)    # 0x1dd3dc
callq 0x111f8c
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq